package com.example.myencryption;

import java.security.InvalidKeyException;
import java.security.NoSuchAlgorithmException;
import java.security.SecureRandom;

import android.support.v7.app.ActionBarActivity;
import android.text.Editable;
import android.text.TextWatcher;


import android.os.Bundle;
import android.util.Base64;
import android.util.Log;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;

import javax.crypto.*;
import javax.crypto.interfaces.*;
import javax.crypto.spec.*;

public class MainActivity extends ActionBarActivity {
	
	private EditText input_text;
	private TextView encrypt_view;
	private TextView decrypt_view;
	private String input= null;
	private SecretKeySpec sks = null;
	private KeyGenerator kg = null;
	private static final String TAG = "com.example.myencryption.ENCRYPTION_EXCEPTION";
	private static final String DEBUG_TAG = "com.example.myencryption.DEBUG_ENV";
	
	private static final int ALGORITHM_SIZE =  128;
	private byte[] encodedInput = null;
	private byte[] decodedOutput = null;
	private Cipher cipher;
	private boolean encStart=false;
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_main);
		Log.d(DEBUG_TAG, "Layout Installed");
		input_text = (EditText) findViewById(R.id.input_text);
		input= input_text.getText().toString();
		
		try 
		{
			SecureRandom sr = SecureRandom.getInstance("SHA1PRNG");
			sr.setSeed("Engin ".getBytes());
			kg = KeyGenerator.getInstance("AES");
			kg.init(ALGORITHM_SIZE, sr);
			sks = new SecretKeySpec((kg.generateKey().getEncoded()), "AES");
			cipher = Cipher.getInstance("AES");
			Log.d(DEBUG_TAG, "Cipher Object Created");
		} 
		catch (NoSuchAlgorithmException exp)
		{
			exp.printStackTrace();
			Log.e(TAG, "No Such Algorithm");
		}
		catch (NullPointerException exp)
		{
			exp.printStackTrace();
			Log.e(TAG, "Null Pointer Exception");
		}
		catch (NoSuchPaddingException exp) {
			exp.printStackTrace();
			Log.e(TAG, "No Such Padding Exception");
		}
	}
	
	
	public void encrypt(View v)
	{
		Log.d(DEBUG_TAG, "Encryption Button Clicked");
		input= input_text.getText().toString();
		
		try {
			cipher.init(Cipher.ENCRYPT_MODE, sks);
			Log.d(DEBUG_TAG, "Encryption Initialized");
			
			encodedInput = cipher.doFinal(input.getBytes());
			Log.d(DEBUG_TAG, "Encryption Done");
			
		} catch (InvalidKeyException exp) {
			exp.printStackTrace();
			Log.e(TAG, "Invalid Key Exception");
		}
		catch (BadPaddingException exp) {
			exp.printStackTrace();
			Log.e(TAG, "Bad Padding Exception");
		}
		catch (IllegalBlockSizeException exp) {
			exp.printStackTrace();
			Log.e(TAG, "Illegal Block Size Exception");
		}
		encrypt_view = (TextView) findViewById(R.id.encrypt_view);
		Log.d(DEBUG_TAG, "Encryption Done1");
		encrypt_view.setText(Base64.encodeToString(encodedInput, Base64.DEFAULT));
		Log.d(DEBUG_TAG, "Encryption Done2");
	}
	
	public void decrypt(View v)
	{
		Log.d(DEBUG_TAG, "Decryption Button Clicked");

		try {
			cipher.init(Cipher.DECRYPT_MODE, sks);
			Log.d(DEBUG_TAG, "Decryption Initialized");
			
			decodedOutput = cipher.doFinal(encodedInput);
			Log.d(DEBUG_TAG, "Decryption Done");
			
		} catch (InvalidKeyException exp) {
			exp.printStackTrace();
			Log.e(TAG, "Invalid Key Exception");
		}
		catch (BadPaddingException exp) {
			exp.printStackTrace();
			Log.e(TAG, "Bad Padding Exception");
		}
		catch (IllegalBlockSizeException exp) {
			exp.printStackTrace();
			Log.e(TAG, "Illegal Block Size Exception");
		}
		decrypt_view = (TextView) findViewById(R.id.decrypt_view);
		Log.d(DEBUG_TAG, "Decryption Done1");
		decrypt_view.setText(new String(decodedOutput));
		Log.d(DEBUG_TAG, "Decryption Done2");
	}
		
	
	
	

	@Override
	public boolean onCreateOptionsMenu(Menu menu) {
		// Inflate the menu; this adds items to the action bar if it is present.
		getMenuInflater().inflate(R.menu.main, menu);
		return true;
	}

	@Override
	public boolean onOptionsItemSelected(MenuItem item) {
		// Handle action bar item clicks here. The action bar will
		// automatically handle clicks on the Home/Up button, so long
		// as you specify a parent activity in AndroidManifest.xml.
		int id = item.getItemId();
		if (id == R.id.action_settings) {
			return true;
		}
		return super.onOptionsItemSelected(item);
	}
}
